package com.pubinfo.passbook.engine.cell.logic.medicine;

import com.pubinfo.passbook.common.model.dto.witeoff.MedicineOnJobOrRetireDTO;
import com.pubinfo.passbook.common.model.dubbo.ESBServiceData;
import com.pubinfo.passbook.common.model.http.engine.ComputeParamSlot;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.engine.constants.ComputeResultType;
import com.pubinfo.passbook.common.utils.ApplicationContextUilts;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 年龄在(min, max)区间内
 *
 * @author zhouch
 * @date 2022/1/19 14:25
 */
public class AgeBetweenUnit extends MedicineLogicUnit {

    private static final ComputeParamSlot MIN_AGE_PARAM_SLOT = ComputeParamSlot.of("min", ComputeResultType.NUMBER);
    private static final ComputeParamSlot MAX_AGE_PARAM_SLOT = ComputeParamSlot.of("max", ComputeResultType.NUMBER);


    DubboService dubboService;

    public AgeBetweenUnit() {

        dubboService = ApplicationContextUilts.getBean(DubboService.class);
    }

    @Override
    public Collection<ComputeParamSlot> getParamSlots() {
        return Arrays.asList(
                MIN_AGE_PARAM_SLOT,
                MAX_AGE_PARAM_SLOT
        );
    }

    @Override
    public Boolean then(MedicineOnJobOrRetireDTO data, BigDecimal result) {
        List<PayInformation> payInformations = cache.getPayInformationData();

        //报账人的身份证号
        String idNo = payInformations.get(0).getPayerPartyCode();

        ESBServiceData.CertDetail certDetail = dubboService.getPersonsDetailByIdNo(idNo);

        //当前日期
        Integer current = Integer.valueOf(new Date().toInstant().toString().substring(0, 10)
                .replaceAll("-", StringUtils.EMPTY));
        //出生日期
        Integer birth = Integer.valueOf(certDetail.getBirthDate()
                .replaceAll("-", StringUtils.EMPTY));
        //年龄
        int age = (current - birth) / 10000;

        int min = getParamenter(data, MIN_AGE_PARAM_SLOT.getName()).intValue();
        int max = getParamenter(data, MAX_AGE_PARAM_SLOT.getName()).intValue();

        return age > min && age < max;
    }

    @Override
    public String getName() {
        return "年龄是否在(" +
                getParamenter(cache.getMedicineData(), MIN_AGE_PARAM_SLOT.getName())
                + "," +
                getParamenter(cache.getMedicineData(), MAX_AGE_PARAM_SLOT.getName())
                + ")";
    }
}
